package boardgamer.common.client.preference;

import android.content.SharedPreferences;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class RegisteredEnumIconList<E extends Enum<E>> extends RegisteredEnumList<E> {

  private final IconListPreference preference;
  private final IconRenderer<E> iconRenderer;
  
  protected RegisteredEnumIconList(
      String key,
      IconListPreference preference,
      E defaultValue,
      Class<E> clazz,
      SharedPreferences sharedPreferences,
      StringConverter<E> stringConverter,
      Renderer<E> renderer,
      IconRenderer<E> iconRenderer) {
    super(key, preference, defaultValue, clazz, sharedPreferences, stringConverter, renderer);
    this.preference = checkNotNull(preference, "preference");
    this.iconRenderer = checkNotNull(iconRenderer, "iconRenderer");
  }

  @Override
  public void setValue(E newValue) {
    super.setValue(newValue);
    preference.setIconViewResource(iconRenderer.getImageResource(newValue));
  }

  public interface IconRenderer<E> {
    int getImageResource(E value);
  }
}
